package com.mllweb.blog.service.impl;

import com.mllweb.blog.core.digest.Base64Utils;
import com.mllweb.blog.core.digest.RSAUtils;
import com.mllweb.blog.core.regex.RegexUtils;
import com.mllweb.blog.data.dao.BlogUserValidateDao;
import com.mllweb.blog.data.orm.BlogUserValidate;
import com.mllweb.blog.service.NoticeService;
import com.mllweb.blog.service.RedisService;
import com.mllweb.blog.service.ValidateService;
import com.mllweb.blog.service.state.ValidateState;
import com.mysql.cj.mysqla.authentication.Sha256PasswordPlugin;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Map;

/**
 * @Prject: blog-server
 * @Package: com.mllweb.blog.service.impl
 * @author: feejson
 * @date: 2017/9/1 16:16
 * @version: V1.0
 */
@Service
public class ValidateServiceImpl implements ValidateService {
    @Autowired
    private NoticeService emailNoticeServiceImpl;
    @Autowired
    private BlogUserValidateDao blogUserValidateDao;
    @Autowired
    private RedisService redisService;
    private final String EMAIL = "email";

    @Async
    @Override
    public void validate(String validateUser) {
        BlogUserValidate validate = new BlogUserValidate();
        validate.setValidateUser(validateUser);
        Calendar calendar = Calendar.getInstance();
        if (RegexUtils.testEmail(validateUser)) {
            validate.setLoginMode(redisService.getLoginMode(EMAIL));
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            validate.setExpiration(calendar.getTime());
            String sign = emailCode(validate);
            blogUserValidateDao.insert(validate);
            emailNoticeServiceImpl.validate(validateUser, sign);
        } else if (RegexUtils.testPhone(validateUser)) {
            //发送手机验证码
        }
    }

    @Override
    public ValidateState validate(String validateUser, String sign) {
        try {
            BlogUserValidate  userValidate = blogUserValidateDao.selectByUser(Base64Utils.decodeAsHex(validateUser));


           if(RSAUtils.verify(userValidate.getValidateUser().getBytes(), userValidate.getValidateCode(), Base64Utils.decodeAsHex(sign))){
               if(userValidate.getExpiration().before(Calendar.getInstance().getTime())){
                   return ValidateState.OVERDUE;
               }else{
                   blogUserValidateDao.updateValidated(userValidate.getId());
                   return ValidateState.SUCCESS;
               }
           }
        } catch (Exception e) {
        }
        return ValidateState.FAILURE;
    }

    private String emailCode(BlogUserValidate validate) {
        try {
            Map<String, Object> keys = RSAUtils.getKey();
            validate.setValidateCode(RSAUtils.getPublicKey(keys));
            return RSAUtils.sign(validate.getValidateUser().getBytes(), RSAUtils.getPrivateKey(keys));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
